package com.jarvis.hospital.contants;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jarvis.hospital.R;

import java.util.List;

/**
 * Contact联系人适配器
 *
 * @author nanchen
 * @fileName WaveSideBarView
 * @packageName com.nanchen.wavesidebarview
 * @date 2016/12/27  15:33
 * @github https://github.com/nanchen2251
 */

public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder> implements View.OnClickListener {

    private List<ContactModel> contacts;
    private static final String TAG = "ContactsAdapter";
    private OnRecyclerviewItemClickListener mOnRecyclerviewItemClickListener = null;
    private Context context;

    public ContactsAdapter(List<ContactModel> contacts,OnRecyclerviewItemClickListener mOnRecyclerviewItemClickListener) {
        this.contacts = contacts;
        this.mOnRecyclerviewItemClickListener = mOnRecyclerviewItemClickListener;
    }

    @Override
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        context=parent.getContext();
        View view = inflater.inflate(R.layout.layaout_item_contacts, null);
        view.setOnClickListener(this);
        return new ContactsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ContactsViewHolder holder, int position) {
        ContactModel contact = contacts.get(position);
        if (position == 0 || !contacts.get(position-1).getIndex().equals(contact.getIndex())) {
            holder.tvIndex.setVisibility(View.VISIBLE);
            holder.tvIndex.setText(contact.getIndex());
        } else {
            holder.tvIndex.setVisibility(View.GONE);
        }
        holder.itemView.setTag(position);
        holder.tvName.setText(contact.getPatientName());
        Glide.with(context)
                .load(contact.getPatientHeadImage())
                .error(R.mipmap.ic_launcher)
                .into(holder.ivAvatar);
    }


    @Override
    public int getItemCount() {
        return contacts.size();
    }

    @Override
    public void onClick(View v) {
        mOnRecyclerviewItemClickListener.onItemClickListener(v, ((int) v.getTag()));
    }

    class ContactsViewHolder extends RecyclerView.ViewHolder {
        TextView tvIndex;
        ImageView ivAvatar;
        TextView tvName;

        ContactsViewHolder(View itemView) {
            super(itemView);
            tvIndex = (TextView) itemView.findViewById(R.id.tv_index);
            ivAvatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
        }
    }
    public interface OnRecyclerviewItemClickListener {
        void onItemClickListener(View v,int position);

    }
}
